package com.chinasofti.suse.ssm.ssmintegrated.web;

import com.chinasofti.suse.ssm.ssmintegrated.common.Consants;
import com.chinasofti.suse.ssm.ssmintegrated.model.User;
import com.chinasofti.suse.ssm.ssmintegrated.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
//
//    @RequestMapping(value = "login", method = RequestMethod.GET)
//    public String toLogin() {
//        return "login";
//    }

    @Autowired
    private UserService userservice;


    @RequestMapping(value = "doLogin",method = RequestMethod.POST)
    public String doLogin(String username, String password, ModelMap modelMap,HttpSession session){
        logger.info("登录参数为：{},{}",username,password);
        try {

            User user = userservice.login(username, password);
            session.setAttribute(Consants.USER_INFO_SK,user);
        }catch (RuntimeException e){
            //登录失败
            logger.warn("登录失败",e);
            modelMap.put("msg","登录失败："+e.getMessage());
            return "login";
        }
        return "redirect:/";
    }


}
